package com.real.operationalcontrols.rxhttp;

import androidx.lifecycle.LifecycleOwner;

import com.rxjava.rxlife.RxLife;

import java.util.List;
import java.util.Map;

import rxhttp.wrapper.annotation.DefaultDomain;
import rxhttp.wrapper.entity.UpFile;
import rxhttp.wrapper.param.RxHttp;

public class RxHttpManger {
    @DefaultDomain
    public static String baseUrl = "http://120.211.100.173:11666/";
    private static volatile RxHttpManger rxHttpManger = null;

    public static RxHttpManger getInstance() {
        if (rxHttpManger == null) {
            synchronized (RxHttpManger.class) {
                if (rxHttpManger == null) {
                    rxHttpManger = new RxHttpManger();
                }
            }
        }
        return rxHttpManger;
    }

    public void post(LifecycleOwner life, String url, Map<String, String> parms,
                     MyDialogObserver observer) {
        //baseUrl = App.getBaseUrl();
        RxHttp.postForm(url)
                .addAll(parms)
                .asString()
                .as(RxLife.asOnMain(life))
                .subscribe(observer);
    }

    public void get(LifecycleOwner life, String url, Map<String, String> parms,
                    MyDialogObserver observer) {
        //baseUrl = App.getBaseUrl();
        RxHttp.get(url)
                .addAll(parms)
                .asString()
                .as(RxLife.asOnMain(life))
                .subscribe(observer);
    }

    public void upLoadFiles(LifecycleOwner life, String url, Map<String, String> parms,
                            List<UpFile> fileList,
                            MyDialogObserver observer) {
        //baseUrl = App.getBaseUrl();
        RxHttp.postForm(url)
                .addAll(parms)
                .addFile(fileList)
                .asString()
                .as(RxLife.asOnMain(life))
                .subscribe(observer);
    }
}
